/**
 * 测试GP服务
 */
define(["dojo/_base/declare",
    "esri/views/2d/draw/Draw",
    "esri/geometry/Point",
    "esri/Graphic",
    "esri/layers/GraphicsLayer",
    "esri/widgets/Expand",
    "esri/widgets/Legend",
    "./js/LoadStatic.js",
    "./js/GdpQuery.js",
    // "./js/custom-measure-tool.js"
  ], function (declare, Draw,Point,Graphic,GraphicsLayer,Expand,Legend,LoadStatic,GdpQuery) {

    return declare(null, {
        /**
         * 成员属性
         * @type {string}
         */
        view:null,//地图视图
        viewPoints:[],//当前视图操作集合
        customMeasureTool:null,
        urls:{
            getToken:'/admin/api/auth',
            jwtTest:'/admin//api/project/test'
        },
        loadStatic:null,
        gdpQuery:null,
        /**
         * 获取素有通用的元素
         * (建议通过function的方式返回，防止因为缓存的原因导致元素事件无法激活)
         * @returns
         */
        elements:function(){
          return {
              J_measure_area:$("#J_measure_area"),
              J_measure_line:$("#J_measure_line"),
          };
        },
        /**
         * 构造函数（初始化函数）
         */
        constructor: function (view) {
            this.view=view;
            this.init();
        },
        /**
         * 初始化函数
         */
        init:function () {
            this.initToken();
            this.initMenu();
            this.initUIEvent();
            this.initOpt();
        },
        initMenu:function () {
            var that=this;
        },

        initOpt:function () {
            var that=this;
            that.loadStatic=new LoadStatic();
            that.gdpQuery=new GdpQuery();
        },
        /**
         * 初始化admin站点的token
         */
        initToken:function(){
            let that=this;
            $.ajax({
                url : that.urls.getToken,
                data : {
                    username:'admin',
                    password:'123456'
                },
                type : 'post',
                dataType : 'json',
                async : true,
                success : function(result) {
                    if(CommonUtils.handlerRes(result)){
                        CommonUtils.TOKEN=result.data;
                        // that.jwtAjaxTest();
                        //catalog.initCatalog();
                        return;
                    }

                },
                error : function(msg) {
                    alert(CommonUtils.getExceptionMsg());
                }
            });
        },

        /**
         * jwt请求测试
         */
        jwtAjaxTest:function(){
            let that=this;
            $.ajax({
                url : that.urls.jwtTest,
                data : {
                },
                type : 'get',
                headers: {
                    'Authorization':CommonUtils.getToken()
                },//通过请求头来发送token
                dataType : 'json',
                async : true,
                success : function(result) {
                    if(CommonUtils.handlerRes(result)){
                        alert(result.msg);
                        return;
                    }

                },
                error : function(msg) {
                    alert(CommonUtils.getExceptionMsg());
                }
            });
        },

        initUIEvent:function () {
            var that=this;
            $("#J_messute_tool_menu").click(function () {
                if($("#J_messute_tool").css("display")=="none"){
                    $("#J_messute_tool").css("display","inline");
                }else {
                    $("#J_messute_tool").css("display","none")
                }
            });

            $("#J_query_tool_menu").click(function () {
                if($("#J_query_tool").css("display")=="none"){
                    $("#J_query_tool").css("display","inline");
                }else {
                    $("#J_query_tool").css("display","none")
                }
            });


            this.elements().J_measure_area.click(function () {
                var params={
                    unit:"平方米",
                    customUnit:"平方米",
                    decimal:"4",
                    coordinates:mapConfig.coordinates
                }

                that.customMeasureTool= new CustomMeasureTool(that.view,"area",params);
            });
            this.elements().J_measure_line.click(function () {
                var params={
                    unit:"米",
                    customUnit:"米",
                    decimal:"4",
                    coordinates:mapConfig.coordinates
                }
                that.customMeasureTool=new CustomMeasureTool(that.view,"length",params);
            });

            //清除
            $("#J_trash").click(function () {
                that.view.graphics.removeAll();
                if( that.customMeasureTool!=null){
                    that.customMeasureTool.clearAllResult();
                }

                //移除所有临时的graphics图层
                var layers=that.view.map.allLayers.items;
                for(var i=layers.length-1;i>=0;i--){
                    var layer=layers[i];
                   if(layer.type=="graphics"){
                        layer.graphics.removeAll();
                        layer.removeAll();
                        // that.view.map.remove(layer);
                    }
                }

            });


            //顶上菜单栏点击状态控制
            $(".head-nav-item").click(function () {

                var year=$(this).attr("data-year");
                var type=$(this).attr("data-type");
                var typeName="";
                if(type=="DQ"){
                    typeName="熵值";
                }else if(type=="TOPSIS"){
                    typeName="TOPSIS值";
                }
                $("#J_taptop_title").html(typeName+"-数据");
                $(".main_table").css("display","block");
                $("#J_taptop_title").attr("type",type);
                that.loadStatic.getStatis(type,year);
            });

            $("#J_taptop_close").click(function () {
                $(".main_table").css("display","none");
            });

        },

    });
});


